package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter07.util.AtguiguUtil;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.io.IOException;
import java.util.List;

/**
 * @Author lzc
 * @Date 2022/7/8 14:11
 */
public class Flink08_StateBackend {
    public static void main(String[] args) throws IOException {
        // ctrl + shift + u
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        env.enableCheckpointing(2000);  // 开启checkpoint
        
        // 1. 内存
        // 1.1 旧
        //        env.setStateBackend(new MemoryStateBackend());  // 默认
        // 1.1 新
        //        env.setStateBackend(new HashMapStateBackend()); // 默认
        //        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
        
        // 2. fs
        
        // 2.1 旧
        //        env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/ck1"));
        // 2.1 新
        //        env.setStateBackend(new HashMapStateBackend());
        //        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck2");
        
        // 3. rocksdb
        // 3.1 旧
        //        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop162:8020/ck3"));
        // 3.1 新
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck4");
        
        
        env
            .socketTextStream("hadoop162", 9999)
            .map(line -> {
                String[] data = line.split(",");
                System.out.println("xxxxx");
                return new WaterSensor(
                    data[0],
                    Long.valueOf(data[1]),
                    Integer.valueOf(data[2])
                );
            })
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                
                
                private MapState<Integer, Object> vcMapState;
                
                @Override
                public void open(Configuration parameters) throws Exception {
                    
                    vcMapState = getRuntimeContext().getMapState(new MapStateDescriptor<Integer, Object>("vcMapState", Integer.class, Object.class));
                    
                }
                
                @Override
                public void processElement(WaterSensor value,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcMapState.put(value.getVc(), new Object());
                    
                    Iterable<Integer> it = vcMapState.keys();
                    
                    List<Integer> list = AtguiguUtil.toList(it);
                    
                    out.collect(ctx.getCurrentKey() + " " + list);
                    
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
}
